﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Xml;
using System.Data;

namespace TestAlbum
{
    class Tools
    {
        #region Setup file read
        public static string GetDatabaseFile
        {
            get
            {
                string configFile = "setup.xml";

                if (File.Exists(configFile))
                {
                    XElement config = XElement.Load(configFile);

                    foreach (XElement c in config.Elements("settings"))
                    {
                        return c.Element("database").Value;
                    }
                }
                else
                    throw new Exception("ERROR : No application file found ! ");

                return "";
            }
        }

        public static string GetPicturePath
        {
            get
            {
                string configFile = "setup.xml";

                if (File.Exists(configFile))
                {
                    XElement config = XElement.Load(configFile);

                    foreach (XElement c in config.Elements("settings"))
                    {
                        return c.Element("picturepath").Value;
                    }
                }
                else
                    throw new Exception("ERROR : No application file found ! ");

                return "";
            }
        }

        public static string DatabaseVersion
        {
            get
            {
                string configFile = Tools.GetDatabaseFile;

                if (File.Exists(configFile))
                {
                    XElement config = XElement.Load(configFile);

                    return config.Element("version").Value;
                }
                else
                    throw new Exception("ERROR : No application file found ! ");

            }
        }

        public static void GetLogoData(out string picture, out string text)
        {
            picture = "";
            text = "";

            string configFile = "setup.xml"; ;

            if (File.Exists(configFile))
            {

                XElement config = XElement.Load(configFile);

                foreach (XElement c in config.Elements("settings"))
                {
                    try
                    {
                        picture = c.Element("logo").Value;
                    }
                    catch { };

                    try
                    {
                        text = c.Element("textLogo").Value;
                    }
                    catch { };
                }
                
            }
            else
                throw new Exception("ERROR : No application file found ! ");
        }

        public static void GetUpdateFolder(out string folder, out string file, out string pictureFolder)
        {
            folder = "";
            file = "";
            pictureFolder = "";

            string configFile = "setup.xml";

            if (File.Exists(configFile))
            {
                XElement config = XElement.Load(configFile);

                foreach (XElement c in config.Elements("settings").Elements("update"))
                {
                    folder = c.Element("folder").Value;
                    file = c.Element("file").Value;
                    pictureFolder = c.Element("picturesFolder").Value;
                }
            }
            else
                throw new Exception("ERROR : No application file found ! ");


        }
        #endregion


        public static bool Updater(string in_fileToUpdate,string in_pictureFolderToUpdate, string in_updateFolder, string in_updateFile, 
            string in_updatePictureFolder)
        {
            bool updated = false;

            string updaterFile = System.IO.Path.Combine(in_updateFolder, in_updateFile);
            string updatePictureFolder = System.IO.Path.Combine(in_updateFolder, in_updatePictureFolder);

            if (File.Exists(updaterFile))
            {
                XElement xmlToUpdate = XElement.Load(in_fileToUpdate);
                XElement xmlUpdater = XElement.Load(updaterFile);

                DateTime lastVersion = Convert.ToDateTime(xmlToUpdate.Element("version").Value);
                DateTime newVersion = Convert.ToDateTime(xmlUpdater.Element("version").Value);

                if (lastVersion >= newVersion)
                    throw new Exception("Le fichier de update est moins récent que la base de donnée actuelle");

                foreach (XElement m in xmlUpdater.Elements("memberList").Elements("member"))
                {
                    xmlToUpdate.Element("memberList").Add(m);
                }
                xmlToUpdate.Save(in_fileToUpdate);
                                
                xmlToUpdate.Element("version").SetValue(newVersion.ToString("yyyy-MM-dd"));

                System.IO.File.Delete(updaterFile);

                updated = true;
            }

            if (!System.IO.Directory.Exists(in_pictureFolderToUpdate))
            {
                System.IO.Directory.CreateDirectory(in_pictureFolderToUpdate);
            }


            if (System.IO.Directory.Exists(updatePictureFolder))
            {
                string[] files = System.IO.Directory.GetFiles(updatePictureFolder);

                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    string fileName = System.IO.Path.GetFileName(s);
                    string destFile = System.IO.Path.Combine(in_pictureFolderToUpdate, fileName);
                    System.IO.File.Copy(s, destFile, true);
                    System.IO.File.Delete(s);

                    updated = true;
                }
            }

            return updated;
        }
    }
}
